<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16" type="flex" justify="center">
      <a-col :order="isMobile ? 2 : 1" :md="24" :lg="16">

        <a-form layout="vertical" :form="form">
			<!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
			<a-form-item v-show="false" label="主键ID">
			  <a-input v-decorator="['id', { initialValue: null }]" disabled />
			</a-form-item>
			
          <a-form-item
            label="姓名"
			required
          >
          <a-input placeholder="请输入姓名" v-decorator="['name',{rules: [{required: true, message: '请输入姓名!'}]}]"/>
          </a-form-item>
		  
          <a-form-item
            label="性别"
			required
          >
            <a-select  placeholder="请选择" default-value="男" v-decorator="['sex',{rules: [{required: true, message: '请选择!'}]}]">
              <a-select-option value="男">男</a-select-option>
              <a-select-option value="女">女</a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item
            label="出生日期"
			required
          >
            <a-date-picker v-decorator="['birth',{rules: [{required: true, message: '请选择!'}]}]"/>
          </a-form-item>
		  
		  <a-form-item
		    label="籍贯"
		  	required
		  >
		  <a-input placeholder="请输入籍贯" v-decorator="['home',{rules: [{required: true, message: '请输入!'}]}]"/>
		  </a-form-item>
		  
		  <a-form-item
		    label="民族"
		  	required
		  >
		    <a-select  placeholder="请选择" default-value="汉族" v-decorator="['nation',{rules: [{required: true, message: '请选择!'}]}]">
		      <a-select-option value="汉族">汉族</a-select-option>
		      <a-select-option value="少数民族">少数民族</a-select-option>
		    </a-select>
		  </a-form-item>

		  <a-form-item
		    label="学历"
		  	required
		  >
		    <a-select  placeholder="请选择" default-value="初中" v-decorator="['degree',{rules: [{required: true, message: '请选择!'}]}]">
		      <a-select-option value="初中">初中</a-select-option>
		      <a-select-option value="高中">高中</a-select-option>
			  <a-select-option value="本科">本科</a-select-option>
			  <a-select-option value="研究生">研究生</a-select-option>
		    </a-select>
		  </a-form-item>
					
          <a-form-item
            label="邮件"
            required
          >
            <a-input placeholder="example@ant.design" v-decorator="['email',{rules: [{required: true, message: '请输入邮件!'}]}]"/>
          </a-form-item>

		  
		  <a-form-item
		    label="电话"
		    required
		  >
		    <a-input placeholder="请输入电话" v-decorator="['phone',{rules: [{required: true, message: '请输入电话!'}]}]"/>
		  </a-form-item>
	
		<a-form-item
		  label="家庭住址"
		  required
		>
		  <a-input placeholder="请输入住址" v-decorator="['address',{rules: [{required: true, message: '请输入住址!'}]}]"/>
		</a-form-item>
		  
		  
		  <a-form-item
		    label="个人介绍"
		  >
		    <a-textarea v-decorator="['desp']"></a-textarea>
		  </a-form-item>
		  
          <a-form-item>
            <a-button type="primary" @click="updateInfo()">更新基本信息</a-button>
          </a-form-item>
        </a-form>

      </a-col>
      <a-col :order="1" :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)" >
          <a-icon type="cloud-upload-o" class="upload-icon"/>
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="option.img"/>
        </div>
      </a-col>

    </a-row>

    <avatar-modal ref="modal" @ok="setavatar"/>

  </div>
</template>

<script>
import AvatarModal from './AvatarModal'
import { baseMixin } from '@/store/app-mixin'
import {updateInfo,findAccount} from '@/api/account'
import { LOGIN_ID } from '@/store/mutation-types'
import storage from 'store'
import pick from 'lodash.pick'
import moment from 'moment'

// 表单字段
const fields = ['desp', 'id', 'name', 'sex', 'birth','home','nation','phone','email','address','degree']
export default {
	name: 'UserInfo',
  mixins: [baseMixin],
  components: {
    AvatarModal
  },
  data () {
    return {
      // cropper
	  model: {},
      preview: {},
      option: {
        img: '/avatar2.jpg',
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      },
	  form: this.$form.createForm(this)
    }
  },
  created() {
	  findAccount(storage.get(LOGIN_ID)).then(res=> {
		  if(res.code == 200){
			  this.model = res.data.userInfo
			  if(res.data.avatar){
				  this.option.img = "http://localhost:8080/static/" + res.data.avatar
			  }
		  }
	  })
	  
	  // 当 model 发生改变时，为表单设置值
	  this.$watch('model', () => {
			if(this.model.birth){
				this.model.birth = moment(new Date(this.model.birth),'YYYY-MM-DD')
			}
	    this.model && this.form.setFieldsValue(pick(this.model, fields))
	  })
  },
  methods: {
    setavatar (avatar) {
      this.option.img = "http://localhost:8080/static/" + avatar
	  console.log(this.option.img)
    },
	updateInfo(){
		this.form.validateFields((errors, values) => {
		  if (!errors) {
		    console.log('values', values)
			const data = {
				account: storage.get(LOGIN_ID),
				user: values
			}
			updateInfo(data).then(res=> {
				if(res.code == 200){
					this.$message.info('修改成功')
				}else{
					this.$message.error('修改失败')
				}
			}).catch(err => {
				this.$message.error('修改失败')
			})
		  } 
		})
	}
  }
}
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
